package com.sun.zxing.util;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.geom.RoundRectangle2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

/**
 * @author Administrator
 */
public class LogoUtil {

    /**
     * 传入logo和二维码 ->
     * @param bufferedImage 二维码
     * @param logo logo图像
     * @return 带logo的二维码
     * @throws IOException
     */
    public static BufferedImage logoMatrix(BufferedImage bufferedImage,String logo) throws IOException {
        //在二维码上画logo:产生一个  二维码画板
        Graphics2D graphics = bufferedImage.createGraphics();

        //画logo： String->BufferedImage(内存)
        BufferedImage logoImg = ImageIO.read(new File(logo));
        //获取原来二维码的图像大小
        int height = bufferedImage.getHeight();
        int width = bufferedImage.getWidth();
        //纯logo图片,xy轴宽高（图片的大小）
        graphics.drawImage(logoImg,width*2/5,height*2/5,width*1/5,height*1/5,null);

        //产生一个 画 白色圆角正方形的 画笔
        BasicStroke basicStroke = new BasicStroke(5,BasicStroke.CAP_ROUND,BasicStroke.JOIN_ROUND);
        //将画板-画笔 关联
        graphics.setStroke(basicStroke);
        //创建一个正方形
        RoundRectangle2D.Float aFloat = new RoundRectangle2D.Float(width * 2 / 5, height * 2 / 5, width * 1 / 5, height * 1 / 5, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND);
        graphics.setColor(Color.white);
        graphics.draw(aFloat);

        //灰色边框
        BasicStroke stroke2 = new BasicStroke(1,BasicStroke.CAP_ROUND ,BasicStroke.JOIN_ROUND) ;
        graphics.setStroke(stroke2);
        //创建一个正方形
        RoundRectangle2D.Float round2 = new RoundRectangle2D.Float(width*2/5+2,height* 2/5+2,    width*1/5-4,height* 1/5 -4, BasicStroke.CAP_ROUND ,BasicStroke.JOIN_ROUND);
        //Color color = new Color(128,128,128) ;
        graphics.setColor(Color.GRAY);
        graphics.draw(round2);

        graphics.dispose();
        bufferedImage.flush();
        return bufferedImage;
    }
}
